package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.app.domain.AppCreditRules;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.mapper.AppActivityMapper;
import com.ruoyi.app.domain.AppActivity;
import com.ruoyi.app.service.IAppActivityService;

/**
 * 活动信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class AppActivityServiceImpl extends ServiceImpl<AppActivityMapper, AppActivity> implements IAppActivityService {
    @Autowired
    private AppActivityMapper appActivityMapper;

    /**
     * 查询活动信息
     *
     * @param activityId 活动信息主键
     * @return 活动信息
     */
    @Override
    public AppActivity selectAppActivityByActivityId(Long activityId) {
        AppActivity entity = appActivityMapper.selectById(activityId);
        return entity;
    }

    /**
     * 查询活动信息列表
     *
     * @param appActivity 活动信息
     * @return 活动信息
     */
    @Override
    public List<AppActivity> selectAppActivityList(AppActivity appActivity) {
        MPJLambdaWrapper<AppActivity> lqw = buildQueryWrapper(appActivity);
        return appActivityMapper.selectList(lqw);
    }

    private MPJLambdaWrapper<AppActivity> buildQueryWrapper(AppActivity appActivity) {
        Map<String, Object> params = appActivity.getParams();
        MPJLambdaWrapper<AppActivity> lqw = new MPJLambdaWrapper<AppActivity>()
                .selectAll(AppActivity.class)
                .select(AppCreditRules::getRuleName,AppCreditRules::getPoints)
                .leftJoin(AppCreditRules.class,AppCreditRules::getRuleId,AppActivity::getRuleId);

        lqw.eq(StringUtils.isNotBlank(appActivity.getTitle()), AppActivity::getTitle, appActivity.getTitle());
        lqw.eq(StringUtils.isNotBlank(appActivity.getDescription()), AppActivity::getDescription, appActivity.getDescription());
        lqw.eq(StringUtils.isNotBlank(appActivity.getCategory()), AppActivity::getCategory, appActivity.getCategory());
        lqw.eq(appActivity.getStartTime() != null, AppActivity::getStartTime, appActivity.getStartTime());
        lqw.eq(appActivity.getEndTime() != null, AppActivity::getEndTime, appActivity.getEndTime());
        lqw.eq(StringUtils.isNotBlank(appActivity.getLocation()), AppActivity::getLocation, appActivity.getLocation());
        lqw.eq(appActivity.getMaxCount() != null, AppActivity::getMaxCount, appActivity.getMaxCount());
        lqw.eq(StringUtils.isNotBlank(appActivity.getStatus()), AppActivity::getStatus, appActivity.getStatus());
        return lqw;
    }

    /**
     * 新增活动信息
     *
     * @param appActivity 活动信息
     * @return 结果
     */
    @Override
    public int insertAppActivity(AppActivity appActivity) {
        appActivity.setCreateBy(SecurityUtils.getUsername());
        appActivity.setCreateTime(DateUtils.getNowDate());
        appActivity.setStatus("0");
        return appActivityMapper.insert(appActivity);
    }

    /**
     * 修改活动信息
     *
     * @param appActivity 活动信息
     * @return 结果
     */
    @Override
    public int updateAppActivity(AppActivity appActivity) {
        appActivity.setUpdateBy(SecurityUtils.getUsername());
        appActivity.setUpdateTime(DateUtils.getNowDate());
        return appActivityMapper.updateById(appActivity);
    }

    /**
     * 批量删除活动信息
     *
     * @param activityIds 需要删除的活动信息主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityByActivityIds(Long[] activityIds) {
        return appActivityMapper.deleteBatchIds(Arrays.asList(activityIds));
    }

    /**
     * 删除活动信息信息
     *
     * @param activityId 活动信息主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityByActivityId(Long activityId) {
        return appActivityMapper.deleteById(activityId);
    }

    @Override
    public int auditActivity(AppActivity appActivity) {
        AppActivity appActivity1 = appActivityMapper.selectById(appActivity.getActivityId());

        appActivity1.setStatus(appActivity.getStatus());
        appActivityMapper.updateById(appActivity1);
        return 1;
    }
}
